package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacAstTestMthDict;
import com.ysd.lis.entity.sta.StaDrugTestMth;
import com.ysd.lis.mapper.bac.BacAstTestMthDictMapper;
import com.ysd.lis.request.bac.BacGermBaseDictRequest;
import com.ysd.lis.service.bac.BacAstTestMthDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author LuoXS
 * @date 2024/3/28
 * version 1.0
 */
@Service
public class BacAstTestMthDictServiceImpl extends ServiceImpl<BacAstTestMthDictMapper, BacAstTestMthDict> implements BacAstTestMthDictService {

    @Autowired
    BacAstTestMthDictMapper mapper;


    @Override
    public Result add(BacAstTestMthDict bacSiteOfInfectionDict) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                bacSiteOfInfectionDict.setCreator(sysUser.getCreator());
                bacSiteOfInfectionDict.setOrgId(sysUser.getOrgId());
            }
            boolean save = this.save(bacSiteOfInfectionDict);
            if (!save) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDict(BacAstTestMthDict bacSiteOfInfectionDict) {
        boolean b = this.updateById(bacSiteOfInfectionDict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result pageList(BacGermBaseDictRequest.BacAstTestMthDictPageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<BacAstTestMthDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            Page<BacAstTestMthDict> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());
            // 参数
            if (!StringUtils.isEmpty(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(wrapper -> wrapper.like(BacAstTestMthDict::getName, pageParam.getSearchValue()).or().like(BacAstTestMthDict::getStandardName, pageParam.getSearchValue()));

            }
            lambdaQueryWrapper.eq(BacAstTestMthDict::getDelFlag, 0);
            lambdaQueryWrapper.orderByAsc(BacAstTestMthDict::getDisplayOrder);
            Page<BacAstTestMthDict> labInstrSetupPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(labInstrSetupPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result findBacAstTestMthDictList(BacAstTestMthDict bacAstTestMthDict) {
        List<BacAstTestMthDict> list = findBacAstTestMthDictArray(bacAstTestMthDict);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacAstTestMthDict> findBacAstTestMthDictArray(BacAstTestMthDict bacAstTestMthDict) {
        MPJLambdaWrapper<BacAstTestMthDict> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacAstTestMthDict::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacAstTestMthDict.getCode()), BacAstTestMthDict::getCode, bacAstTestMthDict.getCode());
        queryWrapper.eq(StringUtils.isNotBlank(bacAstTestMthDict.getName()), BacAstTestMthDict::getName, bacAstTestMthDict.getName());
        queryWrapper.eq(StringUtils.isNotBlank(bacAstTestMthDict.getInputCode()), BacAstTestMthDict::getInputCode, bacAstTestMthDict.getInputCode());
        return mapper.selectList(queryWrapper);
    }

    @Override
    public Result delete(BacGermBaseDictRequest.BacAstTestMthDictDeleteParam deleteParam) {
        Result result;

        try {
            if (ToolsUtils.isEmpty(deleteParam) || ToolsUtils.isEmpty(deleteParam.getIds())) {
                return Result.fail("入参不可为空！");
            }
            String[] array = deleteParam.getIds().split(",");
            boolean b = this.removeByIds(Arrays.asList(array));
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result staDataBacAstTestMthDict(List<StaDrugTestMth> staDrugTestMths) {
        for (StaDrugTestMth staDrugTestMth : staDrugTestMths) {
            BacAstTestMthDict bacAstTestMthDict = new BacAstTestMthDict();
            BeanUtils.copyProperties(staDrugTestMth, bacAstTestMthDict, "id");
            bacAstTestMthDict.setCode(staDrugTestMth.getDrugMthNo());
            bacAstTestMthDict.setName(staDrugTestMth.getDrugMthNa());
            bacAstTestMthDict.setStandardName(staDrugTestMth.getStandardNa());
            bacAstTestMthDict.setDisplayOrder(staDrugTestMth.getSeq());
            this.saveOrUpdate(bacAstTestMthDict, new LambdaQueryWrapper<BacAstTestMthDict>().eq(BacAstTestMthDict::getCode, bacAstTestMthDict.getCode()).eq(BacAstTestMthDict::getDelFlag, 0));
        }
        return Result.succ(1, "同步成功！", null);
    }
}
